package com.bulletelc.apollopre.utils;

import android.graphics.Bitmap;

import com.bulletelc.apollopre.MyApplication;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;

/**
 * author: lhl
 * date  : 2017/11/08
 * desc  : 微信工具类
 */

public final class WXUtil {
    private static final String TYPE_TEXT = "text";
    private static final String TYPE_IMG = "img";
    private static final int THUMB_SIZE = 80;

    private WXUtil() {
        //empty
    }

    /**
     * 分享文本到聊天界面
     * @param text
     */
    public static void shareTextToSession(String text) {
        shareText(text,SendMessageToWX.Req.WXSceneSession);
    }

    /**
     * 分享文本到朋友圈
     * @param text
     */
    public static void shareTextToTimeline(String text) {
        shareText(text,SendMessageToWX.Req.WXSceneTimeline);
    }

    /**
     * 添加文本到微信收藏
     * @param text
     */
    public static void shareTextToFavorite(String text) {
        shareText(text,SendMessageToWX.Req.WXSceneFavorite);
    }

    /**
     * 分享文本类型
     * @param text  文本内容
     * @param scene  场景值
     */
    private static void shareText(String text,int scene) {
        //初始化一个WXTextObject对象,填写分享的文本内容
        WXTextObject textObj = new WXTextObject();
        textObj.text=text;

        //用WXTextObject对象初始化一个WXMediaMessage
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject=textObj;
        msg.description=text;

        //构造一个Reg
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction(TYPE_TEXT);

        req.message=msg;
        req.scene=scene;
        MyApplication.getInstance().getIwxapi().sendReq(req);

    }

    /**
     * 分享图片到聊天界面
     * @param bitmap
     */
    public static void shareImageToSession(Bitmap bitmap) {
        shareImage(bitmap,SendMessageToWX.Req.WXSceneSession);
    }

    /**
     * 分享图片到朋友圈
     * @param bitmap
     */
    public static void shareImageToTimeline(Bitmap bitmap) {
        shareImage(bitmap,SendMessageToWX.Req.WXSceneTimeline);
    }

    /**
     * 添加图片到微信收藏
     * @param bitmap
     */
    public static void shareImageToFavorite(Bitmap bitmap) {
        shareImage(bitmap,SendMessageToWX.Req.WXSceneFavorite);
    }


    /**
     * 分享图片到聊天界面
     * @param bitmap  图片bitmap
     * @param scene  场景值
     */
    private static void shareImage(Bitmap bitmap,int scene) {
        WXImageObject imageObj = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imageObj;

        //设置缩略图
        Bitmap thumbBmp = bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
        //bitmap.recycle();
        msg.thumbData = ImageUtil.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.PNG);

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction(TYPE_IMG);
        req.message=msg;
        req.scene = scene;

        MyApplication.getInstance().getIwxapi().sendReq(req);

    }

    /**
     * 构建一个唯一标志
     * @param type 分享的类型分字符串
     * @return 返回唯一字符串
     */
    private static String buildTransaction(String type) {
        return type == null ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
